#!/usr/bin/perl
# simple Javascript Cloaking
# email cloacking
# by default replaces an email with a mailto link with email cloacked
  
sub emailCloaking {
       # convert text
       my $mail_address = shift;
       my $mailto      = shift; # default is 1
       my $text        = shift; # default is '' 
       my $email       = shift; # default is 1

       $mail_address   = encoding_converter( $mail_address );
       # split email by @ symbol
       @mail           = split(/@/, $mail_address);
       @mail_parts     = split(/./, $mail[1]);
       # random number
       my $rand        = int(rand(99999)) + 1;

       $replacement    = "\n<script language='JavaScript' type='text/javascript'> \n";
       $replacement    .= "<!-- \n";
       $replacement    .= "var prefix = '&#109;a' + 'i&#108;' + '&#116;o'; \n";
       $replacement    .= "var path = 'hr' + 'ef' + '='; \n";
       $replacement    .= "var addy". $rand ." = '". $mail[0] ."' + '&#64;' + '". join( "' + '&#46;' + '", @mail_parts ) ."'; \n";
       if ( $mailto ) {
               # special handling when mail text is different from mail addy
               if ( $text ) {
                       if ( $email ) {
                               # convert text
                               $text   = encoding_converter( $text );
                               # split email by @ symbol
                               @texts   = split(/@/, $text);
                               @text_parts     = split(/./, $texts[1]);
                               $replacement    .= "var addy_text". $rand ." = '". $text[0] ."' + '&#64;' + '". join( "' + '&#46;' + '", $text_parts ) ."'; \n";
                        } else {
                                $text   = encoding_converter( $text );
                                $replacement    .= "var addy_text". $rand ." = '". $text ."';\n";
                        }
                        $replacement    .= "document.write( '<a ' + path + '\'' + prefix + ':' + addy". $rand ." + '\'>' ); \n";
                        $replacement    .= "document.write( addy_text". $rand ." ); \n";
                        $replacement    .= "document.write( '<\/a>' ); \n";
                } else {
                        $replacement    .= "document.write( '<a ' + path + '\'' + prefix + ':' + addy". $rand ." + '\'>' ); \n";
                        $replacement    .= "document.write( addy". $rand ." ); \n";
                        $replacement    .= "document.write( '<\/a>' ); \n";
               }
        } else {
                $replacement    .= "document.write( addy". $rand ." ); \n";
        }
        $replacement    .= "//--> \n";
        $replacement    .= "</script> \n";
        $replacement    .= "<noscript> \n";
        $replacement    .= " -- Email address protected against harvesting -- ";
        $replacement    .= "\n</noscript> \n";

        return $replacement;
}

sub encoding_converter {
        my $text = shift;
	# replace vowels with character encoding
        $text   =~ s/a/&#97;/g;
        $text   =~ s/e/&#101;/g;
        $text   =~ s/i/&#105;/g;
        $text   =~ s/o/&#111;/g;
        $text   =~ s/u/&#117;/g;

        return $text;
}

print emailCloaking("linuxcounter@patrickr.xs4all.nl", 0, '', 1);

